#!/bin/bash
PWD=$(cd $(dirname $0); pwd)
MEM_DIR='/tmp/memory'
MEM_SIZE=$1

[ ! -d ${MEM_DIR} ] && mkdir ${MEM_DIR}
mount -t tmpfs -o size=${MEM_SIZE} tmpfs ${MEM_DIR} >> ${PWD}/mem_up.log 2>&1
if [ $? == 0 ]
then
    dd if=/dev/zero of=${MEM_DIR}/test count=1 bs=${MEM_SIZE} >> ${PWD}/mem_up.log 2>&1
    if [ $? == 0 ]
    then
        sleep 30
        rm ${MEM_DIR}/test && \
        umount ${MEM_DIR} && \
        rm -rf ${MEM_DIR}
    else
        rm ${MEM_DIR}/test && \
        umount ${MEM_DIR} && \
        rm -rf ${MEM_DIR}
        printf "\e[0;31msomething was error , please check ${PWD}/mem_up.log\e[0m\n"
        exit 1
    fi
else
    printf "\e[0;31msomething was error , please check ${PWD}/mem_up.log\e[0m\n"
    exit 2
fi